package com.ethan.http;

import android.net.ParseException;

import com.google.gson.JsonParseException;
import com.google.gson.JsonSyntaxException;

import org.apache.http.conn.ConnectTimeoutException;
import org.json.JSONException;

import java.io.IOException;
import java.net.ConnectException;
import java.net.UnknownHostException;

import retrofit2.HttpException;

public class ApiException extends RuntimeException{
    private static final int UNAUTHORIZED = 401;
    private static final int FORBIDDEN = 403;
    private static final int NOT_FOUND = 404;
    private static final int REQUEST_TIMEOUT = 408;
    private static final int INTERNAL_SERVER_ERROR = 500;
    private static final int BAD_GATEWAY = 502;
    private static final int SERVICE_UNAVAILABLE = 503;
    private static final int GATEWAY_TIMEOUT = 504;

    public int code;
    public String error;

    public ApiException(Throwable cause, int code, String error) {
        super(cause);
        this.code = code;
        this.error = error;
    }

    public ApiException(int code, String error) {
        this.code = code;
        this.error = error;
    }

    public static ApiException handleException(Throwable e) {
        ApiException ex = new ApiException(e, 1, "");
        if (e instanceof HttpException) {
            HttpException httpException = (HttpException) e;
            switch (httpException.code()) {
                case UNAUTHORIZED:
                case FORBIDDEN:
                case NOT_FOUND:
                case REQUEST_TIMEOUT:
                case GATEWAY_TIMEOUT:
                case INTERNAL_SERVER_ERROR:
                case BAD_GATEWAY:
                case SERVICE_UNAVAILABLE:
                default:
                    ex.code = httpException.code();
                    ex.error = "网络错误";
                    break;
            }
            return ex;
        } else if (e instanceof ApiException) {
            ex  = (ApiException)e;
            return ex;
        } else if (e instanceof JsonParseException
                || e instanceof JSONException
                || e instanceof ParseException) {
            ex.code = ApiError.PARSE_ERROR;
            ex.error = "解析错误";
            return ex;
        } else if (e instanceof ConnectException) {
            ex.code = ApiError.NETWORD_ERROR;
            ex.error = "连接失败";
            return ex;
        } else if (e instanceof UnknownHostException) {
            ex.code = ApiError.UNKOWN_HOST;
            ex.error = "未知主机异常";
            return ex;
        } else if (e instanceof IOException) {
            ex.code = ApiError.IO_ERROR;
            ex.error = "I/O异常";
            return ex;
        } else if (e instanceof JsonSyntaxException) {
            ex.code = ApiError.JSONSYNTAX_ERROR;
            ex.error = "Json解析异常";
            return ex;
        } else if (e instanceof IllegalStateException) {
            ex.code = ApiError.ILLEGALSTATE_ERROR;
            ex.error = "IllegalStateException";
            return ex;
        } else if (e instanceof javax.net.ssl.SSLHandshakeException) {
            ex.code = ApiError.SSL_ERROR;
            ex.error = "证书出错";
            return ex;
        } else if (e instanceof ConnectTimeoutException) {
            ex.code = ApiError.TIMEOUT_ERROR;
            ex.error = "连接超时";
            return ex;
        } else if (e instanceof java.net.SocketTimeoutException) {
            ex.code = ApiError.TIMEOUT_ERROR;
            ex.error = "连接超时";
            return ex;
        } else {
            ex.code = ApiError.UNKNOWN;
            ex.error = "未知异常";
            return ex;
        }

    }
}
